package com.itcast.tanhuaserver.controller;

import com.itcast.tanhuaserver.service.CommentsService;
import com.itcast.tanhuaserver.service.MovementsService;
import com.itcast.tanhuaserver.service.QuanziMQService;
import com.itcast.tanhuaserver.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @PachageName: com.itcast.tanhuaserver.controller
 * @ClassName:
 * @Description: TODO
 * @Author: Hushi
 * @DateTime: 2021/2/1812:51
 * @Version 1.0
 */
@RestController
@RequestMapping("comments")
@Slf4j
public class CommentsController {
    @Autowired
    private CommentsService commentsService;

    @Autowired
    private MovementsService movementsService;

    @Autowired
    private QuanziMQService quanziMQService;
    /**
     * 评论列表
     * @param publishId
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> queryCommentsList(@RequestParam("movementId") String publishId,@RequestParam(value = "page",defaultValue = "1")Integer page,@RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){
        try {
            PageResult pageResult =  this.commentsService.queryCommentsList(publishId,page,pageSize);
            if (pageResult != null){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("查询评论列表错误~ publishId:" + publishId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 保存评论(发表)
     * @param parm
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveComments(@RequestBody Map<String,String> parm){
        String movementId = parm.get("movementId");
        String content = parm.get("comment");
        try {
            boolean bool =  this.commentsService.saveComments(movementId,content);
            if (bool){
                this.quanziMQService.commentPublishMsg(movementId);
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("保存评论错误~ movementId:" + movementId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 点赞
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String publishId){
        try {
            Long count = this.movementsService.likeComment(publishId);
            if (count != null){
                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            log.error("点赞功能错误 ~  publishId=" + publishId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 取消点赞
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String publishId){

        try {
            Long count = this.movementsService.disLikeComment(publishId);
            if (count != null){
                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            log.error("取消点赞错误 ~  publishId=" + publishId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
